package com.lyy.service.impl;

import com.lyy.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证成功后要进行的操作,实现spring security提供的接口
 */
@Component("loginSuccessHandler")
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    private IUserDao userDao;

    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //记录相关的用户信息,如上次登录时间
        String name = authentication.getName();
        userDao.updateLastLonginTime(System.currentTimeMillis(),name);

        //调用父类的方法把用户引导到未登录前要去的页面
        super.onAuthenticationSuccess(request,response,authentication);
    }
}
